﻿
using System;

public class Node<T>
{
    public Node<T> Next { get; set; }
    public T Data { get; set; }
    public Node(T t)
    {
        Next = null;
        Data = t;
    }
}


public class GenericList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public GenericList()
    {
        tail = head = null;
    }

    public Node<T> Head { get => head; }

    public void Add(T t)
    {
        Node<T> n = new Node<T>(t);
        if (tail == null)
        {
            head = tail = n;
        }
        else
        {
            tail.Next = n;
            tail = n;
        }
    }

    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        GenericList<int> intList = new GenericList<int>();
        for (int i = 1; i <= 10; i++)
        {
            intList.Add(i);
        }

        Console.WriteLine("链表元素:");
        intList.ForEach(x => Console.Write(x + " "));
        Console.WriteLine();

        int sum = 0;
        intList.ForEach(x => sum += x);
        Console.WriteLine($"总和: {sum}");

        int max = int.MinValue;
        intList.ForEach(x => { if (x > max) max = x; });
        Console.WriteLine($"最大值: {max}");

        int min = int.MaxValue;
        intList.ForEach(x => { if (x < min) min = x; });
        Console.WriteLine($"最小值: {min}");

        GenericList<string> strList = new GenericList<string>();
        strList.Add("Hello");
        strList.Add("C#");
        strList.Add("Generic");

        Console.WriteLine("\n字符串链表:");
        strList.ForEach(s => Console.Write(s + " "));
    }
}